﻿using System;
using System.Text;
using System.Security.Cryptography;

namespace WF.Security
{
    /// <summary>
    /// Cryptography 的摘要说明。

    /// </summary>
    public class Cryptography
    {
        private static byte[] desKey = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };//, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
        private static byte[] desIV = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };//, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};

        public Cryptography()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        // Convert String to byte array
        private static byte[] StringToBytes(string input)
        {
            try
            {
                UnicodeEncoding encoding = new UnicodeEncoding();
                return encoding.GetBytes(input);
            }
            catch
            {
                return null;
            }
        }

        // Convert byte array to string
        private static string BytesToString(byte[] input)
        {
            string rtn;
            try
            {
                UnicodeEncoding encoding = new UnicodeEncoding();
                rtn = new String(encoding.GetChars(input));
            }
            catch
            {
                rtn = null;
            }
            return rtn;
        }

        /// <summary>
        /// 使用MD5算法加密
        /// </summary>
        /// <param name="redundence">冗余值</param>
        /// <param name="plainText">明文</param>
        /// <returns></returns>
        public static string MD5(string redundence, string plainText)
        {
            try
            {
                Byte[] hashByte;
                string rtn = "";
                System.Security.Cryptography.HashAlgorithm hashAlgorithm = (HashAlgorithm)CryptoConfig.CreateFromName("MD5");
                hashByte = hashAlgorithm.ComputeHash(Encoding.ASCII.GetBytes(redundence + plainText));
                for (int i = 0; i < hashByte.Length; i++)
                {
                    rtn = String.Concat(rtn, hashByte[i].ToString("x2"));
                }

                return rtn;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 校验明文与密文

        /// </summary>
        /// <param name="redundence"></param>
        /// <param name="plainText"></param>
        /// <param name="cipherText"></param>
        /// <returns></returns>
        public static bool Verify(string redundence, string plainText, string cipherText)
        {
            return (MD5(redundence, plainText) == cipherText);
        }

        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="plainText"></param>
        /// <returns></returns>
        public static string DESEncrypt(string plainText)
        {
            return plainText;
            //			ICryptoTransform encryptor;
            //			DES des;
            //			byte[] bufPlainText;
            //
            //			if ( (bufPlainText=StringToBytes(plainText)) == null )
            //				return null;
            //			
            //			try
            //			{
            //				des= new DESCryptoServiceProvider();
            //				encryptor = des.CreateEncryptor(desKey,desIV);
            //				return Convert.ToBase64String(encryptor.TransformFinalBlock(bufPlainText,0,bufPlainText.Length));
            //			}
            //			catch
            //			{
            //				return null;
            //			}

        }

        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="cipherText"></param>
        /// <returns></returns>
        public static string DESDecrypt(string cipherText)
        {
            return cipherText;
            //			ICryptoTransform objDecryptor;
            //			DES des;
            //			byte[] bufCipherText;
            //
            //			if ( (bufCipherText=Convert.FromBase64String(cipherText)) == null )
            //				return null;
            //			try
            //			{
            //				des= new DESCryptoServiceProvider();
            //				objDecryptor = des.CreateDecryptor(desKey,desIV);
            //				return BytesToString(objDecryptor.TransformFinalBlock(bufCipherText,0,bufCipherText.Length));
            //			}
            //			catch
            //			{
            //				return null;
            //			}
        }

        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="plainText"></param>
        /// <returns></returns>
        public static string DESEncryption(string plainText)
        {
            //return plainText;
            ICryptoTransform encryptor;
            DES des;
            byte[] bufPlainText;

            if ((bufPlainText = StringToBytes(plainText)) == null)
                return null;

            try
            {
                des = new DESCryptoServiceProvider();
                encryptor = des.CreateEncryptor(desKey, desIV);
                return Convert.ToBase64String(encryptor.TransformFinalBlock(bufPlainText, 0, bufPlainText.Length));
            }
            catch
            {
                return null;
            }

        }

        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="cipherText"></param>
        /// <returns></returns>
        public static string DESDecryption(string cipherText)
        {
            //return cipherText;
            ICryptoTransform objDecryptor;
            DES des;
            byte[] bufCipherText;

            if ((bufCipherText = Convert.FromBase64String(cipherText)) == null)
                return null;
            try
            {
                des = new DESCryptoServiceProvider();
                objDecryptor = des.CreateDecryptor(desKey, desIV);
                return BytesToString(objDecryptor.TransformFinalBlock(bufCipherText, 0, bufCipherText.Length));
            }
            catch
            {
                return null;
            }
        }
    }
}
